package com.sunstar.club.subject.domain.handler.subject.impl;

import com.sunstar.club.subject.common.enums.IsDeletedFlagEnum;
import com.sunstar.club.subject.common.enums.SubjectInfoTypeEnum;
import com.sunstar.club.subject.domain.convert.JudgeConverter;
import com.sunstar.club.subject.domain.convert.MultipleConverter;
import com.sunstar.club.subject.domain.entity.SubjectAnswerBo;
import com.sunstar.club.subject.domain.entity.SubjectInfoBo;
import com.sunstar.club.subject.domain.entity.SubjectOptionBo;
import com.sunstar.club.subject.domain.handler.subject.SubjectTypeHandler;
import com.sunstar.club.subject.infra.basic.entity.SubjectJudge;
import com.sunstar.club.subject.infra.basic.entity.SubjectMultiple;
import com.sunstar.club.subject.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 多选题目的策略类
 *
 * @Author: Shylo Sun
 * @Project: track-club
 * @Date: 2024/2/24 11:12
 */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectMultipleService subjectMultipleService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBo subjectInfoBo) {
        // 多选题目的插入
        List<SubjectMultiple> subjectMultipleList = new LinkedList<>();
        subjectInfoBo.getOptionList().forEach(option -> {
            SubjectMultiple subjectMultiple = MultipleConverter.INSTANCE.convertBoToMultiple(option);
            subjectMultiple.setSubjectId(subjectInfoBo.getId());
            subjectMultiple.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            subjectMultipleList.add(subjectMultiple);
        });
        subjectMultipleService.bathInsert(subjectMultipleList);
    }

    @Override
    public SubjectOptionBo query(int subjectId) {
        SubjectMultiple subjectMultiple = new SubjectMultiple();
        subjectMultiple.setSubjectId(Long.valueOf(subjectId));
        List<SubjectMultiple> result = subjectMultipleService.queryByCondition(subjectMultiple);
        List<SubjectAnswerBo> subjectAnswerBoList = MultipleConverter.INSTANCE.convertMultipleListToBo(result);
        SubjectOptionBo subjectOptionBo = new SubjectOptionBo();
        subjectOptionBo.setOptionList(subjectAnswerBoList);
        return subjectOptionBo;
    }
}
